package com.yungu.swift.order.processor;

import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.SpringContextHolder;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.constants.OrderConstant;
import com.yungu.swift.order.model.vo.PayVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;

/**
 * @author cuixiuyin
 * @date 2017/5/8
 */
public class PayProcessorFactory {

    private static final Logger logger = LoggerFactory.getLogger(PayProcessorFactory.class);

    private static PayProcessor processor;

    //-- 第一级

    /**
     * 入口
     *
     * @return
     */
    public static ResponseData<String> processPay(PayVo payVo) {
        //支付车费
        if (payVo.getPayPurpose() == OrderConstant.PAY_PURPOSE_CHARGE_FARE) {
            if (payVo.getPaySide() == CommonConstant.PASSENGER
                    || payVo.getPaySide() == CommonConstant.PASSENGER_PULBIC
                    || payVo.getPaySide() == CommonConstant.PASSENGER_H5
                    || payVo.getPaySide() == CommonConstant.PASSENGER_APPLET) {
                return processPassengerChargeFare(payVo);
            }
            if (payVo.getPaySide() == CommonConstant.DRIVER) {
                return processDriverChargeFare(payVo);
            }
        }
        //充值
        if (payVo.getPayPurpose() == OrderConstant.PAY_PURPOSE_RECHARGE_ACCOUNT) {
            if (payVo.getPaySide() == CommonConstant.PASSENGER) {
                return processPassengerRecharge(payVo);
            }
        }
        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "不合法的支付目的", payVo.getOrderUuid());
    }


    //-- 第二级

    /**
     * 司机支付车费
     *
     * @return
     */
    private static ResponseData<String> processDriverChargeFare(PayVo payVo) {
        processDriverChargeFareDetail(payVo);
        processCommon(payVo);
        processPassengerGift(payVo);
        if (payVo.getActualFare().compareTo(BigDecimal.ZERO) == 0) {
            //零元支付处理
            return ResponseData.buildSuccessResponse("支付完成", payVo.getOrderUuid());
        }
        return ResponseData.buildSuccessResponse("处理司机支付业务逻辑完毕", payVo.getOrderUuid());
    }

    /**
     * 乘客支付车费
     *
     * @return
     */
    private static ResponseData<String> processPassengerChargeFare(PayVo payVo) {
        //先处理订单状态
        processPassengerChargeFareDetail(payVo);
        processCommon(payVo);
        processPassengerGift(payVo);
        if (payVo.getActualFare().compareTo(BigDecimal.ZERO) == 0) {
            if (payVo.getActualDeductible().compareTo(BigDecimal.ZERO) == 1) {
                //优惠券完全抵扣处理
                return ResponseData.buildSuccessResponse("支付完成，已由优惠券全部抵扣", payVo.getOrderUuid());
            } else {
                //零元支付处理
                return ResponseData.buildSuccessResponse("支付完成", payVo.getOrderUuid());
            }
        }
        return ResponseData.buildSuccessResponse("处理乘客支付业务逻辑完毕", payVo.getOrderUuid());
    }

    /**
     * 乘客充值
     *
     * @return
     */
    private static ResponseData<String> processPassengerRecharge(PayVo payVo) {
        processCommon(payVo);
        processPassengerRechargeDetail(payVo);
        return ResponseData.buildSuccessResponse("处理乘客充值业务逻辑完毕", payVo.getOrderUuid());
    }

    public static void processCommon(PayVo payVo) {

        if (payVo.getPayPurpose() == OrderConstant.PAY_PURPOSE_CHARGE_FARE) {
            Integer typeModule = payVo.getOrderDto().getTypeModule();
            logger.info("------------支付回调>>订单处理完成：" + payVo.getOrderDto().getUuid() + "------------------------");
            //零元支付和余额支付情况不需要处理预支付
            if (payVo.getPayType() != OrderConstant.PAY_TYPE_BALANCE && payVo.getActualFare().compareTo(BigDecimal.ZERO) != 0) {
                logger.info("------------支付回调>>预支付处理START：" + payVo.getOrderDto().getUuid() + "------------------------");
                processPrePay(payVo);
                logger.info("------------支付回调>>预支付处理END：" + payVo.getOrderDto().getUuid() + "------------------------");
            }
            //跨城订单乘客先支付，司机结束订单之后在进行抽成
            if (typeModule != CommonConstant.BUSINESS_TYPE_POOL && typeModule != CommonConstant.BUSINESS_TYPE_PARCEL) {
                processCommission(payVo);
            }
            processOrder(payVo);
            //跨城拼车业务处理分支
            if ((typeModule != CommonConstant.BUSINESS_TYPE_POOL && typeModule != CommonConstant.BUSINESS_TYPE_PARCEL)
                    || (typeModule == CommonConstant.BUSINESS_TYPE_POOL && OrderConstant.ORDER_PAY_PATTERN_AFTER.equals(payVo.getOrderDto().getPayPattern()))) {
                logger.info("------------支付回调>>推送处理START：" + payVo.getOrderDto().getUuid() + "------------------------");
                //跨城支付在派单给司机之前，故无需推送消息给司机
                processPush(payVo);
                logger.info("------------支付回调>>推送处理END：" + payVo.getOrderDto().getUuid() + "------------------------");
            }
            processCache(payVo);
        }
        if (payVo.getPayPurpose() == OrderConstant.PAY_PURPOSE_RECHARGE_ACCOUNT) {
            processOrder(payVo);
            processPrePay(payVo);
            processCache(payVo);
        }
    }

    //-- 叶子级别处理器

    /**
     * 订单处理器
     */
    private static void processCommission(PayVo payVo) {
        processor = SpringContextHolder.getBean(CommissionProcessor.class);
        processor.process(payVo);
    }

    /**
     * 订单处理器
     */
    private static void processOrder(PayVo payVo) {
        processor = SpringContextHolder.getBean(OrderProcessor.class);
        processor.process(payVo);
    }

    /**
     * 推送处理器
     */
    private static void processPush(PayVo payVo) {
        processor = SpringContextHolder.getBean(PushProcessor.class);
        processor.process(payVo);
    }

    /**
     * 预支付处理器
     */
    private static void processPrePay(PayVo payVo) {
        processor = SpringContextHolder.getBean(PrePayProcessor.class);
        processor.process(payVo);
    }

    /**
     * 司机支付车费处理器
     */
    private static void processDriverChargeFareDetail(PayVo payVo) {
        processor = SpringContextHolder.getBean(DriverChargeFareDetailProcessor.class);
        processor.process(payVo);
    }

    /**
     * 乘客支付车费处理器
     */
    private static void processPassengerChargeFareDetail(PayVo payVo) {
        processor = SpringContextHolder.getBean(PassengerChargeFareDetailProcessor.class);
        processor.process(payVo);
    }

    /**
     * 乘客充值处理器
     */
    private static void processPassengerRechargeDetail(PayVo payVo) {
        processor = SpringContextHolder.getBean(PassengerRechargeDetailProcessor.class);
        logger.info("-------调用乘客充值[" + processor.getClass().toString() + "]处理器------------");
        processor.process(payVo);
    }

    /**
     * 礼包处理器
     */
    public static void processPassengerGift(PayVo payVo) {
        processor = SpringContextHolder.getBean(PassengerGiftProcessor.class);
        processor.process(payVo);
    }

    /**
     * 缓存拦截理器
     */
    private static void processCache(PayVo payVo) {
        processor = SpringContextHolder.getBean(CacheProcessor.class);
        processor.process(payVo);
    }

}
